# -*- coding: utf-8 -*-
"""
Created on 2025/7/4 16:56
---------
@summary: 
---------
@author: rui_kai
"""
import os
import queue
import threading
import time

from rich.console import Console
from rich.panel import Panel
from rich.table import Table

from core.change_template import TemplateChange
from core.split_isbn_and_shuffle import SplitIsbnAndShuffle


class SpiderTools:
    counter_lock = threading.Lock()

    EXPORT_PATH = "output"

    def __init__(self):
        self.console = Console()
        self.choice = None
        self.file_name = ""  # 读取的文件名
        self.file_counter = 0
        self.q = queue.Queue()
        self.push_complete = False
        self.write_excel_path = "12"
        self.suffix = None

        self._init()

    def _init(self):
        self.console.print(Panel(
            "初始化程序成功..\n"
            "欢迎使用大象采集/核价 小工具 本软件目前可用功能"
            "结果会在软件当前目录中生成output文件夹"
        ), width=44)

        table = Table(show_header=True, header_style="bold magenta")
        table.add_column("编号", width=4)
        table.add_column("功能", width=40)

        table.add_row("1", "打乱isbn且拆分文件(支持txt;xlsx;csv)")
        table.add_row("2", "核价模板转换为家速云模板(xlsx)")
        table.add_row("3", "核模板转换为其他上货模板(xlsx)")

        self.console.print(table)
        self.function_verify()

        self.main()

    def main(self):
        while True:
            self.file_name = self.console.input("[bold green]请输入文件名(拖入一个文件):[/bold green] ")
            self.file_name = self.file_name.replace('"', "")
            if not os.path.exists(self.file_name):
                self.console.print("[bold red]文件不存在[/bold red]")
                continue
            break

        self.write_excel_path = os.path.splitext(os.path.basename(self.file_name))[0]

        if self.choice == 1:
            SplitIsbnAndShuffle(self.file_name)
            # while True:
            #     spilt_num = self.console.input(f"[bold green]请输入拆分文件数量:[/bold green]")
            #     try:
            #         spilt_num = int(spilt_num)
            #     except ValueError:
            #         self.console.print("[bold red]请输入数字[/bold red]")
            #         continue
            #     break
            #
            # self.console.print(f"[bold green]任务已经开始..请耐心等待...[/bold green]")
            # self.spilt_num = spilt_num
            # self.choices_and_split_file()
        elif self.choice == 2:
            # isbn	售价	运费	总价	售价(你的价格)	利润	图片链接	在售数量	品相	发货时效	店铺成功率
            TemplateChange(
                required_headers=["isbn", "图片链接", "售价"],
                headers=[
                    "ISBN", "售价", "库存", "商品编码(选填)", "书名(选填)", "商品ID(选填)", "diy详情(不要填)",
                    "定价核实(选填)", "主图地址(选填)"
                ],
                file_path=self.file_name,
                suffix="家速云"
            )
            # while True:
            #     stock = self.console.input(f"[bold green]请输入库存数量(回车默认2):[/bold green]") or 2
            #     try:
            #         stock = int(stock)
            #     except ValueError:
            #         self.console.print("[bold red]请输入数字[/bold red]")
            #         continue
            #     break
            #
            # self.console.print(f"[bold green]任务已经开始..请耐心等待...[/bold green]")
            # self.stock = stock
            # self.tem_start()
        elif self.choice == 3:
            # isbn	售价	库存	商家编码	书名	商品ID	diy详情	定价核实	主图地址
            TemplateChange(
                required_headers=["isbn", "图片链接", "售价"],
                headers=["书号", "库存数量", "售价", "商家编码", "书名", "商品图片", "描述"],
                file_path=self.file_name,
                suffix="其他"
            )
            # while True:
            #     stock = self.console.input(f"[bold green]请输入库存数量(回车默认2):[/bold green]") or 2
            #     try:
            #         stock = int(stock)
            #     except ValueError:
            #         self.console.print("[bold red]请输入数字[/bold red]")
            #         continue
            #     break
            #
            # self.console.print(f"[bold green]任务已经开始..请耐心等待...[/bold green]")
            # self.stock = stock
            # self.tem_start()
        else:
            self.console.print("[bold red]功能编号输入错误。程序3秒后退出[/bold red]")
            time.sleep(3)

        self.console.print(f"[bold green]当前任务已经完成，即将在3秒后退出[/bold green]")
        time.sleep(3)

    def function_verify(self):
        while True:
            choice = self.console.input("[bold green]请选择功能编号:[/bold green] ")
            self.console.print(f"您选择了: [yellow]{choice}[/yellow]")
            try:
                choice = int(choice)
            except ValueError:
                self.console.print("[bold red]哎哟喂! 别乱按啊...重新输入吧[/bold red]")
                continue

            if choice not in [1, 2, 3]:
                self.console.print("[bold red]你所选择功能编号还未开发...[/bold red]")
                continue

            self.choice = choice
            break


if __name__ == '__main__':
    SpiderTools()
